package main

import (
	"time"
	"fmt"
)

/*
  装饰着模式
  可以在需要扩展某个类的时候，动态地修改而不需要在内部添加代码，也可以防止类爆炸。
  装饰者模式可以提供了灵活地扩展方案.
  例子： 实现一个日志的自定义功能.
*/

//日志修饰器
type LogDecorator interface {
	Info() string
}
//日志实现
type LogBody struct {
	Msg string
}

func (l LogBody) Info() string {
   return l.Msg
}
//注入时间
type LogTimeField struct {
	dec LogDecorator
}

func (l LogTimeField) Info() string {
	return time.Now().Format("[2006-1-2 15:04:05] ") + l.dec.Info()
}

func NewLogTimeFeild(logDecorator LogDecorator) *LogTimeField {
	return &LogTimeField{logDecorator}
}
//注入名字
type LogNameField struct {
	dec LogDecorator
	name string
}

func (l LogNameField) Info() string {
	return l.name +":"+ l.dec.Info()
}

func NewLogNameFeild(name string, decorator LogDecorator) *LogNameField {
	return &LogNameField{name:name,dec:decorator}
}


func main() {

	msg := "msg"
	name := "name"

	var log LogDecorator
	log = LogBody{Msg:msg}
	log = NewLogTimeFeild(log)
	if name != "" {
		log = NewLogNameFeild(name,log)
	}

	fmt.Println(log.Info())
}
